<?php

namespace frontend\controllers;

/**
 * @author  shuang
 * @date    2016-10-24 16:40:51
 * @version V1.0
 * @desc    
 */
use common\components\WController;
use frontend\models\WB_Vtasklist;
use Yii;
use yii\helpers\Json;
use frontend\models\WB_Task;
use common\components\MTools;
use yii\helpers\ArrayHelper;
use frontend\models\WB_TaskTop;
use common\components\TaskEvent;
use frontend\models\WB_UserTaskOperate;
use yii\helpers\Url;
use common\components\OperateEvent;

class TaskController extends WController {

    public $message = "";
    public $flag = false;

    /*
     * 任务列表
     */

    public function actionMywork() {
        $status = Yii::$app->request->post("status");
        $title = Yii::$app->request->post("title");
        $res = WB_Vtasklist::getList($status, $title);
        return $this->render("mywork", \yii\helpers\ArrayHelper::merge($res, ["status" => $status, "title" => $title]));
    }

    /*
     * 发布任务
     */

    public function actionAdd() {
        $model = new WB_Task();
        $areaType = MTools::getTypeGradePrice();
        if (Yii::$app->request->isAjax) {
            $number = Yii::$app->request->post("number");
            $task_price_id = Yii::$app->request->post("task_price_id");
            $type = Yii::$app->request->post("type");
            $price = MTools::getTaskPrice($task_price_id);
            $taskmoney = $number * $price;
            $event = new TaskEvent();
            $this->on("issue_task", [$event, "issueTask"], [
                "userid" => Yii::$app->user->id,
                "taskmoney" => $taskmoney,
                "number" => $number,
                "operateMsg" => "任务发布",
                "task_price_id" => $task_price_id,
                "type" => 2,
                "class_type" => 4,
                "note" => Yii::$app->request->post("title")
            ]);
            $this->trigger("issue_task", $event);
            $this->off("issue_task");
            echo Json::encode(["status" => $this->flag, "msg" => $this->message, "url" => Url::toRoute(["mywork"])]);
            exit;
        }
        return $this->render("add", ["model" => $model, "areaType" => $areaType]);
    }

    /*
     * 投诉列表
     */

    public function actionTousu() {
        $res = WB_UserTaskOperate::getComplainTaskList();
        return $this->render("tousu", $res);
    }

    /*
     * 追加任务数量
     */

    public function actionZhuijia() {
        $id = Yii::$app->request->get("id");
        $res = WB_Task::find()->where("id=$id")->with(["priceID"])->one();
        if (Yii::$app->request->isAjax) {
            $number = Yii::$app->request->post("number", 0);
            $event = new TaskEvent();
            $this->on("add_task_number", [$event, "addTaskNumber"], [
                "userid" => Yii::$app->user->id,
                "taskmoney" => $number * $res->priceID->price,
                "number" => $number,
                "hidid" => Yii::$app->request->post("hidid"),
                "operateMsg" => "追加任务数量",
                "type" => 2,
                "class_type" => 6,
                "note" => "追加任务:编号" . $res->id . "消耗"
            ]);
            $this->trigger("add_task_number", $event);
            echo Json::encode(["status" => $this->flag, "msg" => $this->message, "url" => Url::toRoute(["mywork"])]);
            exit;
        }
        return $this->render("zhuijia", ["id" => $id, "model" => $res]);
    }

    /*
     * 限速
     */

    public function actionXiansu() {
        $id = Yii::$app->request->get("id");
        $res = WB_Task::find()->where("id=$id")->with(["priceID"])->one();
        if (Yii::$app->request->isAjax) {
            $hidid = Yii::$app->request->post("hidid");
            $model = WB_Task::findOne($hidid);
            $model->is_speed = Yii::$app->request->post("is_speed");
            $model->speed_limit = Yii::$app->request->post("speed_limit", 0);
            if (MTools::saveModel($model) === true) {
                echo Json::encode(["status" => true, "msg" => "操作成功", "url" => Url::toRoute(["mywork"])]);
            } else {
                echo Json::encode(["status" => false, "msg" => "操作失败"]);
            }
            exit();
        }
        return $this->render("xiansu", ["id" => $id, "model" => $res]);
    }

    /*
     * 任务置顶
     */

    public function actionTasktop() {
        $id = Yii::$app->request->get("id");
        $res = WB_Task::find()->where("id=$id")->with(["priceID"])->one();
        $taskTopcount = WB_TaskTop::find()->select("id")->count();
        if (Yii::$app->request->isAjax) {
            $event = new TaskEvent();
            $this->on("add_task_top", [$event, "addTaskTopEvent"], [
                "userid" => Yii::$app->user->id,
                "taskmoney" => 10,
                "hidid" => Yii::$app->request->post("hidid"),
                "operateMsg" => "任务置顶",
                "type" => 2,
                "class_type" => 7,
                "note" => "任务置顶：编号" . $res->id
            ]);
            $this->trigger("add_task_top", $event);
            $this->off("add_task_top");
            echo Json::encode(["status" => $this->flag, "msg" => $this->message, "url" => Url::toRoute(["mywork"])]);
            exit;
        }
        return $this->render("tasktop", ["model" => $res, "id" => $id, "taskTopcount" => $taskTopcount]);
    }

    /*
     * 自动刷新
     */

    public function actionAutoreflash() {
        $id = Yii::$app->request->get("id");
        $res = WB_Task::find()->where("id=$id")->one();
        if (Yii::$app->request->isAjax) {
            $event = new TaskEvent();
            $this->on("reflash_task", [$event, "reflashTask"], [
                "userid" => Yii::$app->user->id,
                "taskmoney" => MTools::getYiiParams("reflash_price") * Yii::$app->request->post("reflashNO"),
                "hidid" => Yii::$app->request->post("hidid"),
                "reno" => Yii::$app->request->post("reflashNO"),
                "operateMsg" => "自动刷新",
                "type" => 2,
                "class_type" => 7,
                "auto" => false,
                "note" => "刷新任务:编号" . $res->id
            ]);
            $this->trigger("reflash_task", $event);
            $this->off("reflash_task");
            echo Json::encode(["status" => $this->flag, "msg" => $this->message, "url" => Url::toRoute(["mywork"])]);
            exit;
        }
        return $this->render("autoreflash", ["id" => $id, "model" => $res]);
    }

    /*
     * 刷新任务
     */

    public function actionReflash() {
        $id = Yii::$app->request->get("id");
        $res = WB_Task::find()->where("id=$id")->one();
        if (Yii::$app->request->isAjax) {
            $event = new TaskEvent();
            $this->on("reflash_task", [$event, "reflashTask"], [
                "userid" => Yii::$app->user->id,
                "taskmoney" => MTools::getYiiParams("reflash_price"),
                "hidid" => Yii::$app->request->post("hidid"),
                "reno" => 1,
                "operateMsg" => "刷新",
                "type" => 2,
                "class_type" => 7,
                "auto" => true,
                "note" => "刷新任务：编号" . $res->id
            ]);
            $this->trigger("reflash_task", $event);
            $this->off("reflash_task");
            echo Json::encode(["status" => $this->flag, "msg" => $this->message, "url" => Url::toRoute(["mywork"])]);
            exit;
        }
        return $this->render("reflash", ["id" => $id, "model" => $res]);
    }

    /*
     * 复制任务
     */

    public function actionCopy() {
        $model = WB_Task::findOne(Yii::$app->request->get("id"));
        $areaType = MTools::getTypeGradePrice();
        return $this->render("add", ["model" => $model, "areaType" => $areaType]);
    }

    /*
     * 修改任务
     */

    public function actionUpdate() {
        $id = Yii::$app->request->get("id");
        $model = WB_Task::findOne($id);
        $areaType = MTools::getTypeGradePrice();
        if (Yii::$app->request->isAjax) {
            $event = new TaskEvent();
            $this->on("edit_task", [$event, "editTask"], [
                "hidid" => $id,
                "userid" => Yii::$app->user->id,
                "operateMsg" => "编辑任务",
            ]);
            $this->trigger("edit_task", $event);
            $this->off("edit_task");
            echo Json::encode(["status" => $this->flag, "msg" => $this->message, "url" => Url::toRoute(["mywork"])]);
            exit;
        }
        return $this->render("update", ["model" => $model, "id" => $id, "areaType" => $areaType]);
    }

    public function actionShowwork() {
        $id = Yii::$app->request->get("id");
        $res = WB_UserTaskOperate::getOperateTask($id);
        return $this->render("showwork", ArrayHelper::merge($res, ["id" => $id]));
    }

    public function actionAjaxcheckupdate() {
        if (Yii::$app->request->isAjax) {
            $event = new TaskEvent();
            $this->on("checkupdate_task", [$event, "checkUpdate"], ["taskmoney" => 0, "userid" => Yii::$app->user->id]);
            $this->trigger("checkupdate_task", $event);
            $this->off("checkupdate_task");
            echo Json::encode(["status" => $this->flag, "msg" => $this->message, "url" => Url::toRoute(["account/pay"])]);
            exit;
        }
    }

    public function actionAjaxchecknumber() {
        $number = Yii::$app->request->post("param");
        if ($number < MTools::getYiiParams("single_task_votes")) {
            echo Json::encode(["status" => "n", "info" => "单次任务大于" . MTools::getYiiParams("single_task_votes") . "票"]);
        } else {
            echo Json::encode(["status" => "y", "info" => ""]);
        }
        exit;
    }

    public function actionAjaxcheckarea() {
        $area = Yii::$app->request->get("area");
        $count = \frontend\models\WB_User::find()->where("login_area like '%$area%'")->count();
        echo Json::encode(["status" => true, "num" => $count]);
        exit;
    }

    public function actionAjaxcomplain() {
        if (Yii::$app->request->isAjax) {
            $id = Yii::$app->request->post("id");
            $cont = Yii::$app->request->post("con");
            $zdy = Yii::$app->request->post("zdy");
            $model = WB_UserTaskOperate::findOne($id);
            $now = time();
            if ($now - $model->uped_at < 86400) {
                $res = WB_UserTaskOperate::complainTask($id, $zdy ? $zdy : $cont);
                if ($res) {
                    $this->flag = true;
                    $this->message = "亲，投诉成功，管理员已经用了洪荒之力审核中！";
                } else {
                    $this->message = "投诉失败";
                }
            } else {
                $this->message = "投诉时间已经超过24小时，不允许再投诉了。";
            }
            echo Json::encode(["status" => $this->flag, "msg" => $this->message]);
            exit;
        }
    }

    public function actionAjaxcomplaintimeout() {
        if (Yii::$app->request->isAjax) {
            $id = Yii::$app->request->post("id");
            $cont = Yii::$app->request->post("con");
            $zdy = Yii::$app->request->post("zdy");
            $model = WB_UserTaskOperate::findOne($id);
            $now = time();
            if ($now - $model->uped_at > 86400) {
                $this->message = "投诉时间已经超过24小时，不允许再投诉了。";
            } else {
                
            }
            echo Json::encode(["status" => $this->flag, "msg" => $this->message]);
            exit;
        }
    }

    public function actionAjaxoperate() {
        $taskid = Yii::$app->request->post("id");
        $action = Yii::$app->request->post("action");
        if ($action === "stoptask") {
            $event = new TaskEvent();
            $this->on("stop_task", [$event, "stopTask"], [
                "hidid" => $taskid,
                "userid" => Yii::$app->user->id,
                "class_type" => 3,
                "type" => 1,
                "operateMsg" => "停止任务",
            ]);
            $this->trigger("stop_task", $event);
            $this->off("stop_task");
            echo Json::encode(["status" => $this->flag, "msg" => $this->message]);
            exit;
        } else if ($action === "dabao") {
            $event = new TaskEvent();
            $this->on("dabao_task", [$event, "dabaoTask"], [
                "hidid" => $taskid,
                "userid" => Yii::$app->user->id,
                "operateMsg" => "打包任务",
            ]);
            $this->trigger("dabao_task", $event);
            $this->off("dabao_task");
            echo Json::encode(["status" => $this->flag, "msg" => $this->message]);
            exit;
        } else if ($action === "dabaodownload") {
            
        } else {
            
        }
    }

}
